package cn.pegasus.teamwork.appinterface.controller.user;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import cn.iyizhan.teamwork.base.util.ObjectUtil;
import cn.pegasus.teamwork.appinterface.base.BaseController;
import cn.pegasus.teamwork.appinterface.controller.user.vo.usr.ReturnRegisterVo;
import cn.pegasus.teamwork.appinterface.session.MySessionContext;
import cn.pegasus.teamwork.appinterface.util.ParameterEunm;
import cn.pegasus.teamwork.appinterface.util.PasswordEncoder;
import cn.pegasus.teamwork.appinterface.util.ReturnRsUtils;
import cn.pegasus.teamwork.appinterface.util.SLEmojiFilterUtils;
import cn.pegasus.teamwork.appinterface.util.ValidatorDataUtil;
import cn.pegasus.teamwork.pegasus.entity.BussUserFundstatistics;
import cn.pegasus.teamwork.pegasus.entity.BussUserInfo;
import cn.pegasus.teamwork.pegasus.entity.BussUserIntegraltotal;
import cn.pegasus.teamwork.pegasus.entity.BussUserOrder;
import cn.pegasus.teamwork.pegasus.service.BussUserFundstatisticsService;
import cn.pegasus.teamwork.pegasus.service.BussUserInfoService;
import cn.pegasus.teamwork.pegasus.service.BussUserIntegraltotalService;
import cn.pegasus.teamwork.pegasus.service.BussUserOrderService;
import cn.pegasus.teamwork.sysconfig.entity.SysConfigDictionary;
import cn.pegasus.teamwork.sysconfig.service.SysConfigDictionaryService;
import cn.pegasus.teamwork.sysconfig.service.SysConfigSmsService;
import cn.pegasus.teamwork.weixinpay.Constant;

/**
 * @ClassName: SysUcenerUserController
 * @Description: 用户控制中心
 * @author ryan.bian
 * @date 2017年11月12日 上午10:10:32
 * 
 *       包括以下主要几个接口：
 * 
 *       1.用户注册 2.用户登录 3.用户第三方接口登录 3.用户修改资料 4.用户手机校验（是否存在唯一性） 5.用户密码修改
 *       6.用户支付密码修改 7.用户退出
 * 
 * 
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@Controller
@RequestMapping("/usr/*")
public class UserInfoController extends BaseController {

	private static final Logger LOGGER = LoggerFactory.getLogger(UserInfoController.class);

	@Autowired
	private BussUserInfoService bussUserInfoService;

	@Autowired
	private BussUserFundstatisticsService bussUserFundstatisticsService;

	@Autowired
	private SysConfigSmsService configSmsService;

	@Autowired
	private BussUserIntegraltotalService integraltotalService;

	@Autowired
	private SysConfigDictionaryService dictionaryService;

	/**
	 * 地区service
	 */
	@Autowired
	private BussUserOrderService bussUserOrderService;

	/**
	 * 用户注册接口
	 * 
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/reg", method = RequestMethod.POST)
	public Object registerUser(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		ReturnRegisterVo vo = new ReturnRegisterVo();
		// 是否为空参数校验
		boolean isNotNull = ObjectUtil
				.checkObjectFile(new String[] { "userMobile", "userPassword", "userFrom", "vcode", "vcodeType" }, map);
		if (isNotNull) {
			String userMobile = map.get("userMobile").toString();
			if (ValidatorDataUtil.isMobile(userMobile)) {
				int vcodeType = Integer.valueOf(map.get("vcodeType").toString());
				// 验证码校验
				String code = map.get("vcode").toString();
				boolean rs = configSmsService.checkSms(Long.valueOf(userMobile), 0L, vcodeType, code);
				if (rs) {
					String userPassword = map.get("userPassword").toString();
					userPassword = PasswordEncoder.getPassword(userPassword);
					Map<String, Object> nap = new HashMap<String, Object>();
					nap.put("userMobile", userMobile);

					BussUserInfo es = bussUserInfoService.detail(nap);
					if (es == null) {
						nap.put("userPassword", userPassword);
						nap.put("userStatus", 2);
						nap.put("userFrom", map.get("userFrom").toString());
						nap.put("userName", "FMDS_" + userMobile);
						nap.put("userRealname", "FMDS_" + userMobile);
						BussUserInfo newuses = bussUserInfoService.add(nap);
						if (newuses != null) {
							// 开通资金总账户
							BussUserFundstatistics bs = new BussUserFundstatistics();
							bs.setUserType(1);
							bs.setObjectId(Long.valueOf(newuses.getId()));
							bussUserFundstatisticsService.add(bs);

							// 开通积分账户
							BussUserIntegraltotal bit = new BussUserIntegraltotal();
							bit.setIntegralTotal(0);
							bit.setUserId(newuses.getId());
							integraltotalService.add(bit);

							// 将数据存储到session中
							HttpSession session = request.getSession();
							session.setAttribute("users", newuses);
							MySessionContext.getInstance().addSession(session);
							setSessionUser(newuses);

							vo.setUcode(newuses.getUserCode());
							vo.setUserImages(newuses.getUserImages());
							vo.setUserMobile(userMobile);
							vo.setUid(newuses.getId().toString());
							vo.setUserCertificate(getSession().getId());
							if (!StringUtils.isEmpty(newuses.getUserPassword())) {
								vo.setIsSetLoginPassword(true);
							} else {
								vo.setIsSetLoginPassword(false);
							}

							if (!StringUtils.isEmpty(newuses.getUserPaypwd())) {
								vo.setIsSetPayPassword(true);
							} else {
								vo.setIsSetPayPassword(false);
							}
							resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, vo);
						} else {
							resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_NULL_RESULT_CODE, vo);
						}

					} else {
						resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_USER_ISEXIT, vo);
					}
				} else {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PHONE_CODE, vo);
				}

			} else {
				resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE, vo);
			}
		} else {
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE, vo);
		}

		return resultMap;
	}

	/**
	 * 用户登录
	 * 
	 * @param userName
	 * @param password
	 * @param app_code
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/lu", method = RequestMethod.POST)
	public Map<String, Object> loginUser(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			ReturnRegisterVo vo = new ReturnRegisterVo();
			// 是否为空参数校验
			boolean isNotNull = ObjectUtil.checkObjectFile(new String[] { "userMobile", "userPassword", "userFrom" },
					map);
			Map<String, Object> rMap = new HashMap<String, Object>();
			rMap.put("dictName", "customTel");

			SysConfigDictionary c = dictionaryService.detail(rMap);
			String customTel = c.getDictValue();
			if (isNotNull) {
				String userMobile = map.get("userMobile").toString();
				String userPassword = map.get("userPassword").toString();
				userPassword = PasswordEncoder.getPassword(userPassword);
				map.put("userPassword", userPassword);
				if (ValidatorDataUtil.isMobile(userMobile)) {
					BussUserInfo newuse = bussUserInfoService.detail(map);
					if (newuse != null) {
						// 将数据存储到session中
						HttpSession session = request.getSession();
						session.setAttribute("users", newuse);
						MySessionContext.getInstance().addSession(session);
						setSessionUser(newuse);

						vo.setUserNick(newuse.getUserNick());
						vo.setUserMobile(userMobile);
						vo.setUcode(newuse.getUserCode());
						vo.setUserImages(newuse.getUserImages());
						vo.setUid(newuse.getId().toString());

						vo.setUserCertificate(getSession().getId());
						if (newuse.getUserMobile() != null) {
							vo.setUserMobile(newuse.getUserMobile());
							vo.setIsbrandPhone(true);
						} else {
							vo.setUserMobile("");
							vo.setIsbrandPhone(false);
						}

						if (!StringUtils.isEmpty(newuse.getUserPassword())) {
							vo.setIsSetLoginPassword(true);
						} else {
							vo.setIsSetLoginPassword(false);
						}

						if (!StringUtils.isEmpty(newuse.getUserPaypwd())) {
							vo.setIsSetPayPassword(true);
						} else {
							vo.setIsSetPayPassword(false);
						}

						vo.setCustomTel(customTel);

						// 通过用户的洗车记录，查询用户的历史订单
						Map<String, Object> lmap = new HashMap<String, Object>();
						lmap.put("userId", newuse.getId());
						BussUserOrder bussUserOrder = bussUserOrderService.findOrderInfoByLogin(lmap);
						if (bussUserOrder != null) {
							vo.setCarBrand(bussUserOrder.getCarBrand());
							vo.setCarCode(bussUserOrder.getCarCode());
							vo.setCarColor(bussUserOrder.getCarColor());
							vo.setCarImages(bussUserOrder.getCarImages());

							vo.setCarModel(bussUserOrder.getCarModel());
							vo.setOrdBalance(bussUserOrder.getOrderBalance().toString());
							vo.setOrdServiceId(bussUserOrder.getProjectId());
							vo.setOrdServiceName(bussUserOrder.getOrderName());
						} else {
							vo.setCarBrand("");
							vo.setCarCode("");
							vo.setCarColor("");
							vo.setCarImages("");

							vo.setCarModel("");
							vo.setOrdBalance("");
							vo.setOrdServiceId("");
							vo.setOrdServiceName("");
						}
						Map<String, Object> mmap = new HashMap<String, Object>();
						mmap.put("objectId", newuse.getId());
						BussUserFundstatistics userFundstatistics = bussUserFundstatisticsService.detail(mmap);
						if (userFundstatistics != null) {
							vo.setUserBalance(userFundstatistics.getBalance().toString());
						} else {
							vo.setUserBalance("0");
						}

						resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, vo);

					} else {
						resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_NULL_RESULT_CODE, vo);
					}
				} else {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE, vo);
				}
			} else {
				resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE, vo);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultMap;
	}

	/**
	 * 用户第三方接口登录
	 * 
	 * @param id
	 * @param map
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/tlu", method = RequestMethod.POST)
	public Map<String, Object> thirdLoginUser(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		ReturnRegisterVo vo = new ReturnRegisterVo();
		try {
			// 是否为空参数校验
			boolean isNotNull = ObjectUtil
					.checkObjectFile(new String[] { "wxUuid", "wxOpenid", "userNick", "userImages" }, map);

			Map<String, Object> rMap = new HashMap<String, Object>();
			rMap.put("dictName", "customTel");

			SysConfigDictionary c = dictionaryService.detail(rMap);
			String customTel = c.getDictValue();
			String userNick = map.get("userNick").toString();
			map.put("userNick", SLEmojiFilterUtils.filterEmoji(userNick));
			if (isNotNull) {
				Map<String, Object> tMap = new HashMap<String, Object>();
				tMap.put("wxOpenid", map.get("wxOpenid"));
				BussUserInfo bi = bussUserInfoService.detail(tMap);
				if (bi != null) {
					// 将数据存储到session中
					HttpSession session = request.getSession();
					session.setAttribute("users", bi);
					MySessionContext.getInstance().addSession(session);
					setSessionUser(bi);
					vo.setUserNick(bi.getUserNick());
					vo.setUserImages(bi.getUserImages());

					vo.setUcode(bi.getUserCode());
					vo.setUid(bi.getId().toString());
					vo.setUserCertificate(getSession().getId());
					if (bi.getUserMobile() != null) {
						vo.setUserMobile(bi.getUserMobile());
						vo.setIsbrandPhone(true);
					} else {
						vo.setUserMobile("");
						vo.setIsbrandPhone(false);
					}

					if (!StringUtils.isEmpty(bi.getUserPassword())) {
						vo.setIsSetLoginPassword(true);
					} else {
						vo.setIsSetLoginPassword(false);
					}

					if (!StringUtils.isEmpty(bi.getUserPaypwd())) {
						vo.setIsSetPayPassword(true);
					} else {
						vo.setIsSetPayPassword(false);
					}

					vo.setCustomTel(customTel);

					// 通过用户的洗车记录，查询用户的历史订单
					Map<String, Object> lmap = new HashMap<String, Object>();
					lmap.put("userId", bi.getId());
					BussUserOrder bussUserOrder = bussUserOrderService.findOrderInfoByLogin(lmap);
					if (bussUserOrder != null) {
						vo.setCarBrand(bussUserOrder.getCarBrand());
						vo.setCarCode(bussUserOrder.getCarCode());
						vo.setCarColor(bussUserOrder.getCarColor());
						vo.setCarImages(bussUserOrder.getCarImages());

						vo.setCarModel(bussUserOrder.getCarModel());
						vo.setOrdBalance(bussUserOrder.getOrderBalance().toString());
						vo.setOrdServiceId(bussUserOrder.getProjectId());
						vo.setOrdServiceName(bussUserOrder.getOrderName());
					} else {
						vo.setCarBrand("");
						vo.setCarCode("");
						vo.setCarColor("");
						vo.setCarImages("");

						vo.setCarModel("");
						vo.setOrdBalance("");
						vo.setOrdServiceId("");
						vo.setOrdServiceName("");
					}
					Map<String, Object> mmap = new HashMap<String, Object>();
					mmap.put("objectId", bi.getId());
					BussUserFundstatistics userFundstatistics = bussUserFundstatisticsService.detail(mmap);
					if (userFundstatistics != null) {
						vo.setUserBalance(userFundstatistics.getBalance().toString());
					} else {
						vo.setUserBalance("0");
					}

					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, vo);
				} else {
					String tuid = map.get("wxUuid").toString();
					map.put("userName", "FMDS_" + tuid);
					map.put("userStatus", 2);
					map.put("userFrom", 3);
					BussUserInfo bio = bussUserInfoService.add(map);
					if (bio != null) {
						// 开通资金总账户
						BussUserFundstatistics bs = new BussUserFundstatistics();
						bs.setUserType(1);
						bs.setObjectId(Long.valueOf(bio.getId()));
						bussUserFundstatisticsService.add(bs);

						// 开通积分账户
						BussUserIntegraltotal bit = new BussUserIntegraltotal();
						bit.setIntegralTotal(0);
						bit.setUserId(bio.getId());
						integraltotalService.add(bit);

						// 将数据存储到session中
						HttpSession session = request.getSession();
						session.setAttribute("users", bio);
						MySessionContext.getInstance().addSession(session);

						setSessionUser(bio);

						vo.setUserNick(bio.getUserNick());
						vo.setUserImages(bio.getUserImages());
						if (bio.getUserMobile() != null) {
							vo.setUserMobile(bio.getUserMobile());
							vo.setIsbrandPhone(true);
						} else {
							vo.setUserMobile("");
							vo.setIsbrandPhone(false);
						}
						vo.setCustomTel(customTel);
						vo.setUcode(bio.getUserCode());
						vo.setUid(bio.getId().toString());
						vo.setUserCertificate(getSession().getId());

						if (!StringUtils.isEmpty(bio.getUserPassword())) {
							vo.setIsSetLoginPassword(true);
						} else {
							vo.setIsSetLoginPassword(false);
						}

						if (!StringUtils.isEmpty(bio.getUserPaypwd())) {
							vo.setIsSetPayPassword(true);
						} else {
							vo.setIsSetPayPassword(false);
						}
						vo.setCarBrand("");
						vo.setCarCode("");
						vo.setCarColor("");
						vo.setCarImages("");

						vo.setCarModel("");
						vo.setOrdBalance("");
						vo.setOrdServiceId("");
						vo.setOrdServiceName("");
						vo.setUserBalance("0");
						resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, vo);
					}
				}
			} else {
				resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE, vo);
			}
		} catch (Exception e) {
			LOGGER.error("修改用户失败.", e);
		}
		return resultMap;
	}

	/**
	 * 此接口提供用户修改资料（修改昵称，修改头像，绑定手机）
	 * 
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/uu", method = RequestMethod.POST)
	public Map<String, Object> updateUser(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 是否为空参数校验 userNick/userImages/userPassword/userPaypwd/userMobile
		int type = Integer.valueOf(map.get("type").toString());
		int id = Integer.valueOf(getSessionUserId().toString());
		if (id > 0) {
			map.put("id", id);
			if (type == 1) {
				boolean isNotNull = ObjectUtil.checkObjectFile(new String[] { "userNick" }, map);
				if (isNotNull) {
					bussUserInfoService.update(map);
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, true);
				} else {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE, false);
				}
			} else if (type == 2) {
				boolean isNotNull = ObjectUtil.checkObjectFile(new String[] { "userImages" }, map);
				if (isNotNull) {
					bussUserInfoService.update(map);
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, true);
				} else {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE, false);
				}
			} else if (type == 3) {
				boolean isNotNull = ObjectUtil.checkObjectFile(new String[] { "userMobile", "vcode", "vcodeType" },
						map);
				if (isNotNull) {
					// 验证手机验证码，
					int vcodeType = Integer.valueOf(map.get("vcodeType").toString());
					long userMobile = Long.valueOf(map.get("userMobile").toString());
					long userId = getSessionUserId();
					String code = map.get("vcode").toString();
					boolean rs = configSmsService.checkSms(userMobile, userId, vcodeType, code);
					if (rs) {
						bussUserInfoService.update(map);
						resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, true);
					} else {
						resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PHONE_CODE, false);
					}
				} else {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE, false);
				}
			}
		} else {
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE, false);
		}

		return resultMap;
	}

	/**
	 * 用户密码修改、用户支付密码修改、忘记登录密码、忘记支付密码
	 * 
	 * @param id
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/uup", method = RequestMethod.POST)
	public Map<String, Object> updateUserPwd(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();

		boolean isNotNull = ObjectUtil.checkObjectFile(new String[] { "userMobile", "vcode", "type", "vcodeType" },
				map);
		if (isNotNull) {
			// 是否为空参数校验 userNick/userImages/userPassword/userPaypwd/userMobile
			int type = Integer.valueOf(map.get("type").toString());
			int id = Integer.valueOf(getSessionUserId().toString());
			// 验证手机验证码，
			long userMobile = Long.valueOf(map.get("userMobile").toString());
			long userId = getSessionUserId();
			int vcodeType = Integer.valueOf(map.get("vcodeType").toString());
			String code = map.get("vcode").toString();
			boolean rs = configSmsService.checkSms(userMobile, userId, vcodeType, code);
			if (rs) {
				if (id > 0) {
					Map<String, Object> tMap = new HashMap<String, Object>();

					tMap.put("userMobile", userMobile);
					BussUserInfo newuses = bussUserInfoService.detail(map);
					if (newuses != null) {
						if (type == 1) {
							boolean isNotNull1 = ObjectUtil
									.checkObjectFile(new String[] { "userPassword", "newUserPassword", "vcode" }, map);
							if (isNotNull1) {
								// 验证手机验证码，
								BussUserInfo bu = new BussUserInfo();
								Map<String, Object> rtMap = new HashMap<String, Object>();
								rtMap.put("id", id);

								String userPassword = map.get("userPassword").toString();
								userPassword = PasswordEncoder.getPassword(userPassword);
								bu.setUserPassword(userPassword);
								rtMap.put("userPassword", userPassword);

								bu = bussUserInfoService.detail(rtMap);
								if (bu != null) {
									String newUserPassword = map.get("newUserPassword").toString();
									newUserPassword = PasswordEncoder.getPassword(newUserPassword);
									rtMap.put("userPassword", newUserPassword);
									bussUserInfoService.update(rtMap);
									resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, true);
								} else {
									resultMap = ReturnRsUtils.resultBackString(ParameterEunm.FAILED_CODE, false);
								}
							} else {
								resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE, false);
							}
						} else if (type == 2) {
							boolean isNotNull2 = ObjectUtil
									.checkObjectFile(new String[] { "userPaypwd", "newuserPaypwd", "vcode" }, map);
							if (isNotNull2) {
								// 验证手机验证码，
								BussUserInfo bu = new BussUserInfo();
								Map<String, Object> rtMap = new HashMap<String, Object>();
								rtMap.put("id", id);

								String userPassword = map.get("userPaypwd").toString();
								userPassword = PasswordEncoder.getPassword(userPassword);
								bu.setUserPassword(userPassword);
								rtMap.put("userPaypwd", userPassword);

								bu = bussUserInfoService.detail(rtMap);
								if (bu != null) {
									String newUserPassword = map.get("newuserPaypwd").toString();
									newUserPassword = PasswordEncoder.getPassword(newUserPassword);
									rtMap.put("userPaypwd", newUserPassword);
									bussUserInfoService.update(rtMap);
									resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, true);
								} else {
									resultMap = ReturnRsUtils.resultBackString(ParameterEunm.FAILED_CODE, false);
								}
							} else {
								resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE, false);
							}
						} else {

						}
					} else {
						resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_USER_PARAMS_CODE, false);
					}
				} else {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE, false);
				}
			} else {
				resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_405_CODE, false);
			}
		} else {
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE, false);
		}

		return resultMap;
	}

	/**
	 * 用户密码修改、用户支付密码修改、忘记登录密码、忘记支付密码
	 * 
	 * @param id
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/fup", method = RequestMethod.POST)
	public Map<String, Object> forgetUserPwd(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();

		boolean isNotNull = ObjectUtil.checkObjectFile(new String[] { "userMobile", "vcode", "type", "vcodeType" },
				map);
		if (isNotNull) {
			// 是否为空参数校验 userNick/userImages/userPassword/userPaypwd/userMobile
			int type = Integer.valueOf(map.get("type").toString());
			// 验证手机验证码，
			long userMobile = Long.valueOf(map.get("userMobile").toString());
			int vcodeType = Integer.valueOf(map.get("vcodeType").toString());
			String code = map.get("vcode").toString();
			boolean rs = configSmsService.checkSms(userMobile, 0l, vcodeType, code);
			if (rs) {
				if (ValidatorDataUtil.isMobile(String.valueOf(userMobile))) {
					// 验证码校验

					if (type == 1) {
						boolean isNotNull3 = ObjectUtil.checkObjectFile(new String[] { "forgetUserPassword", "vcode" },
								map);
						if (isNotNull3) {
							// 验证手机验证码，
							Map<String, Object> rtMap = new HashMap<String, Object>();
							rtMap.put("userMobile", userMobile);

							String newUserPassword = map.get("forgetUserPassword").toString();
							newUserPassword = PasswordEncoder.getPassword(newUserPassword);
							rtMap.put("userPassword", newUserPassword);

							bussUserInfoService.updateUserPawd(rtMap);
							resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, true);
						} else {
							resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE, false);
						}
					} else if (type == 2) {
						boolean isNotNull4 = ObjectUtil.checkObjectFile(new String[] { "forgetuserPaypwd", "vcode" },
								map);
						if (isNotNull4) {
							// 验证手机验证码，
							Map<String, Object> rtMap = new HashMap<String, Object>();
							rtMap.put("userMobile", userMobile);

							String newUserPassword = map.get("forgetuserPaypwd").toString();
							newUserPassword = PasswordEncoder.getPassword(newUserPassword);
							rtMap.put("userPaypwd", newUserPassword);

							boolean rsu = bussUserInfoService.updateUserPawd(rtMap);
							if (rsu) {
								resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, true);
							} else {
								resultMap = ReturnRsUtils.resultBackString(ParameterEunm.FAILED_CODE, false);
							}

						} else {
							resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE, false);
						}
					}

				} else {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_USER_PARAMS_CODE, false);
				}
			} else {
				resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_405_CODE, false);
			}
		} else {
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE, false);
		}
		return resultMap;
	}

	/**
	 * 用户手机校验（是否存在唯一性）
	 * 
	 * @param userMobile_number
	 * @param app_code
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/cp", method = RequestMethod.POST)
	public Map<String, Object> checkuserMobile(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 是否为空参数校验
		boolean isNotNull = ObjectUtil.checkObjectFile(new String[] { "userMobile" }, map);
		if (isNotNull) {
			String userMobile = map.get("userMobile").toString();
			if (ValidatorDataUtil.isMobile(userMobile)) {
				// 验证码校验
				BussUserInfo newuses = bussUserInfoService.detail(map);
				if (newuses != null) {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, false);
				} else {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, true);
				}
			}
		} else {
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE, false);
		}

		return resultMap;
	}

	/**
	 * 退出账户
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/eu", method = RequestMethod.POST)
	public Map<String, Object> exitUser(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		HttpSession session = request.getSession();
		// 将数据存储到session中
		String sessionId = request.getParameter("userCertificate");
		MySessionContext.getInstance().DelSession(session);
		HttpSession nsession = MySessionContext.getInstance().getSession(sessionId);
		if (nsession == null) {
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, true);
		} else {
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.FAILED_CODE, false);
		}
		return resultMap;
	}

	/**
	 * 退出账户
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/cku", method = RequestMethod.POST)
	public Map<String, Object> checkUser(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 将数据存储到session中

		if (getSessionUserId() != null) {
			int id = Integer.valueOf(getSessionUserId().toString());
			if (id > 0) {
				resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, true);
			} else {
				resultMap = ReturnRsUtils.resultBackString(ParameterEunm.FAILED_CODE, false);
			}
		} else {
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.FAILED_CODE, false);
		}

		return resultMap;
	}

	/**
	 * 订单数据自动填充
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/daf", method = RequestMethod.POST)
	public Map<String, Object> dataFull(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 将数据存储到session中
		ReturnRegisterVo vo = new ReturnRegisterVo();
		if (getSessionUserId() != null) {
			// 通过用户的洗车记录，查询用户的历史订单
			Map<String, Object> lmap = new HashMap<String, Object>();
			int id = Integer.valueOf(getSessionUserId().toString());
			lmap.put("userId", id);
			BussUserOrder bussUserOrder = bussUserOrderService.findOrderInfoByLogin(lmap);
			if (bussUserOrder != null) {
				vo.setCarBrand(bussUserOrder.getCarBrand());
				vo.setCarCode(bussUserOrder.getCarCode());
				vo.setCarColor(bussUserOrder.getCarColor());
				vo.setCarImages(bussUserOrder.getCarImages());

				vo.setCarModel(bussUserOrder.getCarModel());
				vo.setOrdBalance(bussUserOrder.getOrderBalance().toString());
				vo.setOrdServiceId(bussUserOrder.getProjectId());
				vo.setOrdServiceName(bussUserOrder.getOrderName());
			} else {
				vo.setCarBrand("");
				vo.setCarCode("");
				vo.setCarColor("");
				vo.setCarImages("");

				vo.setCarModel("");
				vo.setOrdBalance("");
				vo.setOrdServiceId("");
				vo.setOrdServiceName("");
			}
			lmap.put("rs", vo);
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, lmap);
		} else {
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.FAILED_CODE, false);
		}

		return resultMap;
	}

	/**
	 * 获取用户的TOKEN
	 * 
	 * @param userMobile_number
	 * @param app_code
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/gat", method = RequestMethod.POST)
	public Map<String, Object> getAccessToken(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();

		// 是否为空参数校验
		boolean isNotNull = ObjectUtil.checkObjectFile(new String[] { "code" }, map);
		if (isNotNull) {
			try {
				HttpClient client = new HttpClient();
				// 设置代理服务器地址和端口

				String APPID = Constant.APP_ID;
				String SECRET = Constant.APP_SECRET;
				String access_result = "";
				String aturl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + APPID + "&secret=" + SECRET
						+ "&grant_type=authorization_code&code=" + map.get("code");

				// 使用 GET 方法 ，如果服务器需要通过 HTTPS 连接，那只需要将下面 URL 中的 http 换成 https
				HttpMethod method = new GetMethod(aturl);
				// 使用POST方法
				// HttpMethod method = new PostMethod("http://java.sun.com");
				client.executeMethod(method);

				// 打印服务器返回的状态
				System.out.println(method.getStatusLine());
				if (method.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
					// 打印返回的信息
					System.out.println(method.getResponseBodyAsString());
					Map<String, String> m = (Map<String, String>) JSON.parse(method.getResponseBodyAsString());
					// TODO
					String access_token = m.get("access_token");
					String openid = m.get("openid");

					if (access_token != null && openid != null) {
						String url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid="
								+ openid + "&lang=zh_CN";

						// 使用 GET 方法 ，如果服务器需要通过 HTTPS 连接，那只需要将下面 URL 中的 http 换成
						// https
						HttpMethod method2 = new GetMethod(url);
						// 使用POST方法
						client.executeMethod(method2);

						// 打印服务器返回的状态
						System.out.println(method2.getStatusLine());
						if (method2.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
							// 打印返回的信息
							System.out.println(method2.getResponseBodyAsString());
							access_result = method2.getResponseBodyAsString();
							String json = new String(access_result.getBytes("ISO-8859-1"), "UTF-8");
							Map<String, Object> tMap = new HashMap<String, Object>();
							tMap.put("rs", json);

							resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, tMap);
						}
					} else {
						resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_NULL_RESULT_CODE, null);
					}

				}
				// 释放连接
				method.releaseConnection();

			} catch (IOException e) {
				resultMap = ReturnRsUtils.resultBackString(ParameterEunm.FAILED_CODE, null);
			}
		} else {
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE, false);
		}

		return resultMap;
	}

	
	public static void main(String[] args) {
		 String str = "城市狼人";  
		 String str2 = str.replaceAll(" ", "");   
	        System.out.println(str2);   
	}
	
}
